package com.ruoyi.alarm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.alarm.domain.AlarmRule;
import com.ruoyi.alarm.service.IAlarmRuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 告警规则Controller
 * 
 * @author yepanpan
 * @date 2025-03-10
 */
@RestController
@RequestMapping("/alarm/rule")
public class AlarmRuleController extends BaseController
{
    @Autowired
    private IAlarmRuleService alarmRuleService;

    /**
     * 查询告警规则列表
     */
    @PreAuthorize("@ss.hasPermi('alarm:rule:list')")
    @GetMapping("/list")
    public TableDataInfo list(AlarmRule alarmRule)
    {
        startPage();
        List<AlarmRule> list = alarmRuleService.selectAlarmRuleList(alarmRule);
        return getDataTable(list);
    }

    /**
     * 导出告警规则列表
     */
    @PreAuthorize("@ss.hasPermi('alarm:rule:export')")
    @Log(title = "告警规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlarmRule alarmRule)
    {
        List<AlarmRule> list = alarmRuleService.selectAlarmRuleList(alarmRule);
        ExcelUtil<AlarmRule> util = new ExcelUtil<AlarmRule>(AlarmRule.class);
        util.exportExcel(response, list, "告警规则数据");
    }


    /**
     * 获取告警规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('alarm:rule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(alarmRuleService.selectAlarmRuleById(id));
    }

    /**
     * 新增告警规则
     */
    @PreAuthorize("@ss.hasPermi('alarm:rule:add')")
    @Log(title = "告警规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody AlarmRule alarmRule)
    {
        return toAjax(alarmRuleService.insertAlarmRule(alarmRule));
    }

    /**
     * 修改告警规则
     */
    @PreAuthorize("@ss.hasPermi('alarm:rule:edit')")
    @Log(title = "告警规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody AlarmRule alarmRule)
    {
        return toAjax(alarmRuleService.updateAlarmRule(alarmRule));
    }

    /**
     * 删除告警规则
     */
    @PreAuthorize("@ss.hasPermi('alarm:rule:remove')")
    @Log(title = "告警规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(alarmRuleService.deleteAlarmRuleByIds(ids));
    }
}
